<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreateAdminUsersRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'password' => 'required|min:6|confirmed:password_confirmation',
            'email'     => 'required|email',
            'mobile'   => 'required|digits:11'
        ];
        if ($this->isMethod('post')){
            $rules['email'] = 'required|unique:admin_users|email';
        }elseif ($this->isMethod('patch') && $this->input('password') == null){
            unset($rules['password']);
        }
        return $rules;
    }

    public function messages(){
        return [
            'email.required'  => '邮箱必填',
            'email.unique'    => '邮箱已使用，请重新填写',
            'email.email'       => '邮箱格式不正确',
            'password.min'       => '密码至少6位',
            'password.required'  => '密码必填',
            'password.confirmed' => '两次密码输入不一致',
            'name.required'      => '姓名必填',
            'name.min'           => '姓名至少2位',
            'mobile.required'    => '手机号必填',
            'mobile.digits'      => '手机号码格式或长度不正确',
        ];
    }
}
